package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.entity.*;
import com.qf.mapper.OrderAddressDao;
import com.qf.service.GoodsService;
import com.qf.service.OrdersService;
import com.qf.utils.DateUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

@CrossOrigin("*")
@RestController
@RequestMapping("/orders")
public class OrderController {

    private OrdersService ordersService;
    private GoodsService goodsService;
    private OrderAddressDao orderAddressDao;
    public OrderController(OrdersService ordersService,GoodsService goodsService,OrderAddressDao orderAddressDao){

        this.ordersService = ordersService;
        this.goodsService= goodsService;
        this.orderAddressDao = orderAddressDao;
    }
    @GetMapping
    @ResponseBody
    private List<WebOrder> getOrders(HttpSession session, Integer status,Integer id) throws ServletException, IOException {
//        String status = req.getParameter("status");
//        得到user对象属性的值
//        User user = (User) req.getSession().getAttribute("user");
//        Integer userId = user.getUId();


        User user = (User) session.getAttribute("user");



        return  ordersService.getOrdersByStatus(status,user.getUId()) ;

    }

    @GetMapping("/ChangeStatus")
    @ResponseBody
    private List<Orders> ChangeStatus(HttpSession session, String no ,Integer status)throws ServletException, IOException{

        return ordersService.ChangeStatus(no,status);
    }



    @PostMapping
    private Object insertOrder(HttpSession session,String ordersS,Integer addres){
        System.out.println("创建订单-----"+ordersS);
        Map<String,Integer> orderMap = (Map<String, Integer>) JSON.parse(ordersS);
        orderMap.forEach((k,v)->{
            System.out.println("key:"+ k+"\t"+v);
        });
        Map<String, Object> map = new HashMap<>();
        if (orderMap.size()<1){
            map.put("code",0);
            map.put("msg","购物车没商品！！！");
            return map;
        }
        User user = (User) session.getAttribute("user");
        int num = (int) ((Math.random() * 9 + 1) * 1000000);
        Date date = new Date();
        String orderNo = DateUtils.transferDateToStr(date,DateUtils.DATE_KEY_STR);
        orderNo = orderNo+num;

        List<Orders> ordersList = new ArrayList<>();
        Set<Map.Entry<String, Integer>> entries = orderMap.entrySet();
        for (Map.Entry<String, Integer> oneMap:entries) {
            Orders orders = new Orders();
            Goods goods = goodsService.queryByGoodsId(oneMap.getKey());
            orders.setOGoodId(oneMap.getKey());
            orders.setONum(oneMap.getValue());
            orders.setONo(orderNo);
            orders.setOCreateTime(date);
            orders.setOStatus(0);
            orders.setOGoodPrice(new BigDecimal(goods.getPrice()));
            orders.setOGoodTitle(goods.getTitle());
            orders.setOGoodImg(goods.getSmallPic());
            orders.setOUserId(user.getUId());
            ordersList.add(orders);

        }
        Integer integer = null;
        try {
            integer = ordersService.batchInsert(ordersList);
            OrderAddress orderAddress = new OrderAddress();
            orderAddress.setONo(orderNo);
            orderAddress.setOAddress(addres);
            orderAddressDao.insert(orderAddress);

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (integer>0){
            map.put("code",1);
            map.put("msg","订单提交成功！！！");
            map.put("data",orderNo);
            return map;
        }else {
            map.put("code",0);
            map.put("msg","订单提交失败！！！");
            return map;
        }
    }

}

